@isTest

Private class iFulfillHandlerTest{


    static testMethod void test_Handler()

    {
        Profile p = [select id from profile where name='System Administrator'];           
        User u = new User(alias = 'isysadm', email='isysadmin@accenture.com',emailencodingkey='UTF-8', lastname='Tes12', languagelocalekey='en_US',localesidkey='en_US', profileid = p.Id,timezonesidkey='Asia/Calcutta', username='itestad123@testorg.com');           
        Insert u;
        
        Workflow_definition__c wfd = new Workflow_definition__c();
           wfd.Status__c = 'Active';
           wfd.Workflow_Name__c = 'WF';
           insert wfd;

       Workflow_Stage__c WFS = new Workflow_Stage__c();
            WFS.name = 'Advertiser Approval';
            WFS.Workflow_Code__c = wfd.id;
            WFS.Sequence__c = 2;
            insert WFS;
      Wokflow_Steps__c wfSteps = new Wokflow_Steps__c();
            wfSteps.name = 'Final Verification';
            wfSteps.Wokflow_Stage_Name__c = WFS.id;
            wfSteps.Sequence__c=1;
            wfSteps.Target_Days__c = 12;
            insert wfSteps;
       

        Fullfilment_Item__c FI = new Fullfilment_Item__c();
            FI.Current_Stage__c = WFS.id;
            FI.Workflow_Code__c = wfd.id;
            FI.Current_Step__c = wfSteps.id;
            //FI.ownerId = [select id from User where ];
            
            system.runAs(u){
            insert FI;}
            
        List<Fullfilment_Item__c> FIList = new List<Fullfilment_Item__c>();
        FIList.add(FI);
        
        Set<ID> fiIDs = new Set<ID>();
        fiIDs.add(FI.id);
        
         iFulfillHandler.IsRecursive();
         iFulfillHandler.setRecursive(true);
         //iFulfillHandler.copyOwner(FIList);
         iFulfillHandler.updateLastTaskAndFunction(wfd.id);
         iFulfillHandler.closeQueryRequestAndQueries(fiIDs);              
    }
    
    static testMethod void test_fulfillmentFunctionDefinitionTrigger(){
    
        list<Stage_Definition__c> preStageDefinitionList = new list<Stage_Definition__c>();
        list<Workflow_Stage__c> workflowStageList= new list<Workflow_Stage__c>();
        
        Workflow_Definition__c testWD = new Workflow_Definition__c();
        testWD.Workflow_Name__c = 'Testing the Defination Creation'; 
        testWD.Workflow_Description__c = 'This is the workflow defination description to ensure test covereage';
        testWD.Version__c = 1.0; 
        testWD.Status__c = 'Build In Progress';
        insert testWD;
        
        Stage_Definition__c newPDF1 = new Stage_Definition__c(name = 'T1',Stage_Description__c ='Test Function 1',Sequence__c = 55.0, Status__c ='Active' );
        Stage_Definition__c newPDF2 = new Stage_Definition__c(name = 'T2',Stage_Description__c ='Test Function 2',Sequence__c = 56.0, Status__c ='Active' );
        Stage_Definition__c newPDF3 = new Stage_Definition__c(name = 'T3',Stage_Description__c ='Test Function 3',Sequence__c = 57.0, Status__c ='Active' );
        
        preStageDefinitionList.add(newPDF1);
        preStageDefinitionList.add(newPDF2);
        preStageDefinitionList.add(newPDF3);       
                
        insert preStageDefinitionList;  
                
                        
        Workflow_Stage__c newF1 = new Workflow_Stage__c(name='WS1',Stage__c=preStageDefinitionList[0].id, Sequence__c = 1.0, Workflow_Code__c =testWD.id);
        Workflow_Stage__c newF2 = new Workflow_Stage__c(name='WS2',Stage__c=preStageDefinitionList[1].id, Sequence__c = 2.0, Workflow_Code__c =testWD.id);
        Workflow_Stage__c newF3 = new Workflow_Stage__c(name='WS3',Stage__c=preStageDefinitionList[2].id, Sequence__c = 3.0, Workflow_Code__c =testWD.id);
           
        
        workflowStageList.add(newF1);
        workflowStageList.add(newF2);
        workflowStageList.add(newF3);       
        
        insert workflowStageList;
        
        preStageDefinitionList[0].name = 'Testing';
        preStageDefinitionList[1].name = 'Testing1';
        preStageDefinitionList[2].name = 'Testing2';           
                        
        update preStageDefinitionList;

        
    }
    static testMethod void test_fulfillmentStepDefinition(){
       list<Step_Definition__c> preTaskDefList= new list<Step_Definition__c>();
       list<Wokflow_Steps__c> preWorkflowDefList = new list<Wokflow_Steps__c>();
       
        Workflow_Definition__c testWD = new Workflow_Definition__c();
        testWD.Workflow_Name__c = 'Testing the Defination Creation'; 
        testWD.Workflow_Description__c = 'This is the workflow defination description to ensure test covereage';
        testWD.Version__c = 1.0; 
        testWD.Status__c = 'Build In Progress';
        insert testWD;       

       
       Stage_Definition__c newPDF1 = new Stage_Definition__c(name = 'F1',Stage_Description__c ='Test Function 1',Sequence__c = 85.0, Status__c ='Active' );
       insert newPDF1 ;
            
       Workflow_Stage__c newF1 = new Workflow_Stage__c(name='WS1',Stage__c=newPDF1.id, Sequence__c = 1.0, Workflow_Code__c =testWD.id);
       insert newF1;
               
       Step_Definition__c newPTD1 = new Step_Definition__c(name='s1',Step_Description__c = 'S1',Step_Sequence__c = 1.0, Status__c = 'Active',Target_Days__c =1.0,Stage__c = newPDF1.id);
       Step_Definition__c newPTD2 = new Step_Definition__c(name='s1',Step_Description__c = 'S2',Step_Sequence__c = 2.0, Status__c = 'Active',Target_Days__c =2.0,Stage__c = newPDF1.id);
       Step_Definition__c newPTD3 = new Step_Definition__c(name='s1',Step_Description__c = 'S3',Step_Sequence__c = 3.0, Status__c = 'Active',Target_Days__c =1.0,Stage__c = newPDF1.id);          
       Step_Definition__c newPTD4 = new Step_Definition__c(name='s1',Step_Description__c = 'S4',Step_Sequence__c = 4.0, Status__c = 'Active',Target_Days__c =2.0,Stage__c = newPDF1.id);          
         
       preTaskDefList.add(newPTD1);
       preTaskDefList.add(newPTD2);
       preTaskDefList.add(newPTD3);
       preTaskDefList.add(newPTD4);
          
       insert preTaskDefList;
       
       Wokflow_Steps__c newWT1 = new Wokflow_Steps__c(Wokflow_Stage_Name__c = newF1.id ,Required__c = true, Sequence__c = 1.0, name = 'workflow steps Task 1', Target_Days__c =1.0,Step__c = preTaskDefList[0].id);
       Wokflow_Steps__c newWT2 = new Wokflow_Steps__c(Wokflow_Stage_Name__c = newF1.id ,Required__c = false,Sequence__c = 2.0, name = 'workflow steps Task 2', Target_Days__c =3.0,Step__c = preTaskDefList[0].id);
       Wokflow_Steps__c newWT3 = new Wokflow_Steps__c(Wokflow_Stage_Name__c = newF1.id ,Required__c = false,Sequence__c = 3.0, name = 'workflow steps Task 3', Target_Days__c =2.0,Step__c = preTaskDefList[0].id);
       Wokflow_Steps__c newWT4 = new Wokflow_Steps__c(Wokflow_Stage_Name__c = newF1.id ,Required__c = false,Sequence__c = 4.0, name = 'workflow steps Task 4', Target_Days__c =1.0,Step__c = preTaskDefList[0].id);
        
       preWorkflowDefList.add(newWT1);
       preWorkflowDefList.add(newWT2);
       preWorkflowDefList.add(newWT3);
       preWorkflowDefList.add(newWT4);
       
       insert preWorkflowDefList;
              
       preTaskDefList[0].name='Task1';
       preTaskDefList[1].name='Task2';
       preTaskDefList[2].name='Task3';
       preTaskDefList[3].name='Task4';
       
       update preTaskDefList;
        
    }

}